/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.gui.setting;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import com.jeestrap.common.smartcoder.database.DbUtil;
import com.jeestrap.common.smartcoder.domain.DbTable;
import com.jeestrap.common.smartcoder.domain.OrmTableField;
import com.jeestrap.common.smartcoder.gui.setting.jtable.OrmTableModel;
import com.jeestrap.common.smartcoder.util.StrUtils;

public class OrmSettingPanel extends JPanel {

	/**
	 * serialVersionUID long
	 */
	private static final long serialVersionUID = 9118502214727418104L;

	JComboBox tableNameComboBox = new JComboBox();

	JTextField functionIdTextField = new JTextField();

	JTextField functionNameTextField = new JTextField();

	OrmTableModel voFieldModel;

	JTable fieldTable;

	JButton addFieldButton;

	JButton delFieldButton;

	public OrmSettingPanel() {
		super();
		setLayout(new GridBagLayout());

		JPanel ormHeadPanel = initOrmHeadSettingPanel();
		add(ormHeadPanel, new GridBagConstraints(0, 0, 1, 1, 0.00D, 0.00D, GridBagConstraints.WEST,
			GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

		JPanel ormTablePanel = initOrmTablePanel();
		add(ormTablePanel, new GridBagConstraints(0, 1, 1, 1, 0.01D, 0.01D, 17, 1, new Insets(0, 0, 0, 0), 0, 0));

		JPanel ormButtonPanel = initOrmButtonPanel();
		add(ormButtonPanel, new GridBagConstraints(0, 2, 1, 1, 0.00D, 0.00D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
	}

	/**
	 * 初始化保存代码配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initOrmHeadSettingPanel() {
		JPanel ormHeadPanel = new JPanel();
		ormHeadPanel.setLayout(new GridBagLayout());

		TitledBorder titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145,
			140)), "映射表配置");
		ormHeadPanel.setBorder(titledBorder4);

		JLabel tableNameLabel = new JLabel("数据库表:");
		GridBagConstraints gbc_tableNameLabel = new GridBagConstraints();
		gbc_tableNameLabel.insets = new Insets(0, 0, 5, 5);
		gbc_tableNameLabel.gridx = 0;
		gbc_tableNameLabel.gridy = 0;
		ormHeadPanel.add(tableNameLabel, gbc_tableNameLabel);

		this.tableNameComboBox.setEditable(true);
		tableNameComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent paramAnonymousItemEvent) {
				if (tableNameComboBox.getSelectedItem() != null
					& (tableNameComboBox.getSelectedItem() instanceof DbTable)) {
					DbTable dbTable = (DbTable)tableNameComboBox.getSelectedItem();

					String str = dbTable.getTblName();
					String comment = dbTable.getTblComments();
					if ((str != null) && (str.trim().length() > 0)) {
						try {
							List<OrmTableField> arrayOfTableField = DbUtil.getTableColumnsByTable(str);
							OrmSettingPanel.this.voFieldModel.removeAll();
							for (OrmTableField dbColumn : arrayOfTableField) {
								OrmSettingPanel.this.voFieldModel.addRow(dbColumn);
								OrmSettingPanel.this.voFieldModel.fireTableDataChanged();
							}
						} catch (Throwable localThrowable) {
							localThrowable.printStackTrace();
							OrmSettingPanel.this.tableNameComboBox.requestFocus();
							JOptionPane.showMessageDialog(OrmSettingPanel.this, localThrowable.getMessage(), "Error", 0);
						}

						// 设置Java类名
						String javaClz = StrUtils.substringBefore(str, "_")
							+ StrUtils.capitalize(StrUtils.substringAfterLast(str, "_"));
						OrmSettingPanel.this.functionIdTextField.setText(javaClz);
						OrmSettingPanel.this.functionNameTextField.setText(comment);

						// 预览按钮
						Container c = (Container)OrmSettingPanel.this.getParent().getParent().getComponent(2);
						c.getComponent(0).setEnabled(true);
					}
				} else {
					// 预览按钮
					Container c = (Container)OrmSettingPanel.this.getParent().getParent().getComponent(2);
					c.getComponent(0).setEnabled(false);
				}
			}
		});

		GridBagConstraints gbc_tableNameComboBox = new GridBagConstraints();
		gbc_tableNameComboBox.insets = new Insets(0, 0, 5, 5);
		gbc_tableNameComboBox.gridx = 1;
		gbc_tableNameComboBox.gridy = 0;
		gbc_tableNameComboBox.anchor = GridBagConstraints.WEST;
		ormHeadPanel.add(tableNameComboBox, gbc_tableNameComboBox);

		JLabel functionIdLabel = new JLabel("Java类名:");
		GridBagConstraints gbc_functionIdLabel = new GridBagConstraints();
		gbc_functionIdLabel.insets = new Insets(0, 0, 5, 5);
		gbc_functionIdLabel.gridx = 2;
		gbc_functionIdLabel.gridy = 0;
		ormHeadPanel.add(functionIdLabel, gbc_functionIdLabel);

		functionIdTextField.setColumns(20);
		GridBagConstraints gbc_functionIdTextField = new GridBagConstraints();
		gbc_functionIdTextField.insets = new Insets(0, 0, 5, 5);
		gbc_functionIdTextField.gridx = 3;
		gbc_functionIdTextField.gridy = 0;
		ormHeadPanel.add(functionIdTextField, gbc_functionIdTextField);

		JLabel functionNameLabel = new JLabel("功能名称:");
		GridBagConstraints gbc_functionNameLabel = new GridBagConstraints();
		gbc_functionNameLabel.insets = new Insets(0, 0, 5, 5);
		gbc_functionNameLabel.gridx = 4;
		gbc_functionNameLabel.gridy = 0;
		ormHeadPanel.add(functionNameLabel, gbc_functionNameLabel);

		functionNameTextField.setColumns(20);
		GridBagConstraints gbc_functionNameTextField = new GridBagConstraints();
		gbc_functionNameTextField.insets = new Insets(0, 0, 5, 5);
		gbc_functionNameTextField.gridx = 5;
		gbc_functionNameTextField.gridy = 0;
		ormHeadPanel.add(functionNameTextField, gbc_functionNameTextField);

		return ormHeadPanel;
	}

	/**
	 * 初始化保存代码配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initOrmTablePanel() {
		JPanel ormTablePanel = new JPanel();
		ormTablePanel.setLayout(new GridBagLayout());

		TitledBorder titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145,
			140)), "映射属性配置");
		ormTablePanel.setBorder(titledBorder4);

		voFieldModel = new OrmTableModel();
		fieldTable = new JTable(voFieldModel);
		setTableColumnModel();

		JScrollPane jScrollPane1 = new JScrollPane();
		jScrollPane1.setViewportView(this.fieldTable);

		ormTablePanel.add(jScrollPane1, new GridBagConstraints(0, 0, 1, 1, 0.01D, 0.01D, 10, 1, new Insets(5, 5, 5, 5),
			0, 0));
		jScrollPane1.setViewportView(this.fieldTable);

		return ormTablePanel;
	}

	/**
	 * 初始化保存代码配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initOrmButtonPanel() {
		JPanel ormButtonPanel = new JPanel();
		ormButtonPanel.setLayout(new GridBagLayout());

		addFieldButton = new JButton("增加属性");
		addFieldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// OrmSettingPanel.this.voFieldModel.addRow(new OrmTableField());
				JOptionPane.showMessageDialog(OrmSettingPanel.this, "开发中...", "提示", 1);
			}
		});

		GridBagConstraints gbc_addFieldButton = new GridBagConstraints();
		gbc_addFieldButton.insets = new Insets(5, 5, 5, 5);
		gbc_addFieldButton.gridx = 0;
		gbc_addFieldButton.gridy = 0;
		ormButtonPanel.add(addFieldButton, gbc_addFieldButton);

		delFieldButton = new JButton("删除属性");
		delFieldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// if (OrmSettingPanel.this.fieldTable.getSelectedRow() >= 0)
				// OrmSettingPanel.this.voFieldModel.removeRow(OrmSettingPanel.this.fieldTable.getSelectedRow());
				JOptionPane.showMessageDialog(OrmSettingPanel.this, "开发中...", "提示", 1);
			}
		});
		GridBagConstraints gbc_delFieldButton = new GridBagConstraints();
		gbc_delFieldButton.insets = new Insets(5, 5, 5, 5);
		gbc_delFieldButton.gridx = 1;
		gbc_delFieldButton.gridy = 0;
		ormButtonPanel.add(delFieldButton, gbc_delFieldButton);

		return ormButtonPanel;
	}

	private void setTableColumnModel() {

		JComboBox editTypeComboBox = new JComboBox();
		JComboBox javaTypeComboBox = new JComboBox();
		JComboBox dataTypeComboBox = new JComboBox();

		editTypeComboBox.addItem("text");
		editTypeComboBox.addItem("combox");
		editTypeComboBox.addItem("checkbox");
		editTypeComboBox.addItem("radio");
		DefaultCellEditor localDefaultCellEditor1 = new DefaultCellEditor(javaTypeComboBox);
		DefaultCellEditor localDefaultCellEditor2 = new DefaultCellEditor(dataTypeComboBox);
		DefaultCellEditor localDefaultCellEditor3 = new DefaultCellEditor(editTypeComboBox);
		TableColumnModel localTableColumnModel = this.fieldTable.getColumnModel();
		localTableColumnModel.getColumn(8).setCellEditor(localDefaultCellEditor1);
		localTableColumnModel.getColumn(12).setCellEditor(localDefaultCellEditor2);
		localTableColumnModel.getColumn(13).setCellEditor(localDefaultCellEditor3);
		javaTypeComboBox.setEditable(true);
		javaTypeComboBox.addItem("boolean");
		javaTypeComboBox.addItem("java.lang.Boolean");
		javaTypeComboBox.addItem("byte");
		javaTypeComboBox.addItem("java.lang.Byte");
		javaTypeComboBox.addItem("short");
		javaTypeComboBox.addItem("java.lang.Short");
		javaTypeComboBox.addItem("char");
		javaTypeComboBox.addItem("java.lang.Character");
		javaTypeComboBox.addItem("int");
		javaTypeComboBox.addItem("java.lang.Integer");
		javaTypeComboBox.addItem("long");
		javaTypeComboBox.addItem("java.lang.Long");
		javaTypeComboBox.addItem("float");
		javaTypeComboBox.addItem("java.lang.Float");
		javaTypeComboBox.addItem("double");
		javaTypeComboBox.addItem("java.lang.Double");
		javaTypeComboBox.addItem("double");
		javaTypeComboBox.addItem("java.lang.String");
		javaTypeComboBox.addItem("java.util.Date");
		javaTypeComboBox.setSelectedItem("java.lang.string");
		dataTypeComboBox.setEditable(true);
		dataTypeComboBox.addItem(" ");
		dataTypeComboBox.addItem("int");
		dataTypeComboBox.addItem("number");
		dataTypeComboBox.addItem("email");
		dataTypeComboBox.addItem("mobile");
		dataTypeComboBox.addItem("telphone");
		dataTypeComboBox.addItem("qq");
		dataTypeComboBox.addItem("ip");
		dataTypeComboBox.addItem("port");
		dataTypeComboBox.addItem("money");
		dataTypeComboBox.addItem("date");
		dataTypeComboBox.setSelectedItem(" ");
		DefaultTableCellRenderer localDefaultTableCellRenderer = new DefaultTableCellRenderer();
		localDefaultTableCellRenderer.setHorizontalAlignment(0);
		this.fieldTable.getTableHeader().setReorderingAllowed(false);
	}
}
